package com.rd.student.jiaji.class3.wangyanbo.task27;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class TestStream {

    public static void main(String[] args) throws Exception{

//        testRead1();
        testRead2();
    }
    public static void testRead()throws Exception{
        // File file=new File ("stream.txt");
        InputStream is = new FileInputStream("stream.txt");
        //read()
        //一个字节一个字节读取数据
        while (true){
            //反复调用此方法 知道返回-1为止
            int len = is.read();
            if (len==-1)break;

            byte[] arr = new byte[1];
            arr[0] = (byte) len;
            System.out.println(new String(arr));
        }
        is.close();
    }
    public static void testRead1()throws Exception{
//        File file=new File ("stream.txt");
        InputStream is = new FileInputStream("stream.txt");
        //read()
        //一个字节一个字节读取数据
        int cnt = 0;
        while (true){
            byte[] arr = new byte[28];
            int len = is.read(arr);
            if (len==-1){
                System.out.println();
                System.out.println(cnt);
                System.out.println("读取完成");
                break;}
            String content = new String(arr);
            System.out.print(len + "--" + content);
            cnt++;
        }
        System.out.println();
        System.out.println(cnt);
        is.close();
    }
    public static void testClose(){
        InputStream is = null;
        try{
            is=new FileInputStream("stream.txt");
            int cnt=0;
            while (true){
                byte[] arr = new byte[28];
                int len=is.read(arr);
                if (len==-1){
                    System.out.println();
                    System.out.println(cnt);
                    System.out.println("读取完成");
                    break;
                }
                String content = new String(arr);
                System.out.println(len + "--"+content);
                cnt++;
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();;
        }finally {
            try{
                is.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    //自动关闭
    public static void testAutoClose(){
        //程序里打开的IO流不属于内存资源 垃圾回收机制无法回收
        //所以需要手动关闭
        //因为手动关闭流资源非常繁琐 又容易忘记
        //jdk7以后提供了一种自动关闭 IO流资源的机制
        InputStream is = null;
        try{
            is=new FileInputStream("stream.txt");
            int cnt=0;
            while (true){
                byte[] arr = new byte[28];
                int len=is.read(arr);
                if (len==-1){
                    System.out.println();
                    System.out.println(cnt);
                    System.out.println("读取完成");
                    break;
                }
                String content = new String(arr);
                System.out.println(len + "--"+content);
                cnt++;
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();;
        }

    }
    public static void testRead2()throws Exception{
//        File file=new File ("stream.txt");
        InputStream is = new FileInputStream("stream.txt");
        //read()
        //一个字节一个字节读取数据
        int cnt = 0;
        while (true){
            byte[] arr = new byte[28];
            int len = is.read(arr);
            if (len==-1){
                System.out.println();
                System.out.println(cnt);
                System.out.println("读取完成");
                break;}
            String content = new String(arr,0,len);
            System.out.print(len + "--" + content);
            cnt++;
        }
        is.close();
    }
}
